package com.sky.service.impl;

import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;

@Service
@Slf4j
@RequiredArgsConstructor
public class WorkspaceServiceImpl implements WorkspaceService {
    private final WorkspaceMapper workspaceMapper;

    @Override
    public BusinessDataVO businessData() {
        BusinessVO businessVO = workspaceMapper.businessData();
        if (businessVO.getSumAmount() == null) {
            businessVO.setSumAmount(0.00);
        }
        // 当天订单完成率
        Double orderCompletionRate = 0.00;
        if (businessVO.getCountAll().doubleValue() != 0){
            orderCompletionRate = businessVO.getCountEffective().doubleValue() / businessVO.getCountAll().doubleValue();
        }

        Double unitPrice = 0.000;
        if (businessVO.getCountEffective().doubleValue() != 0) {
            // 平均客价
            unitPrice = businessVO.getSumAmount() / businessVO.getCountEffective().doubleValue();
        }
        DecimalFormat df = new DecimalFormat("#.00");
        Double formattedUnitPrice = Double.valueOf(df.format(unitPrice));

        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setTurnover(businessVO.getSumAmount());
        businessDataVO.setValidOrderCount(businessVO.getCountEffective());
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        businessDataVO.setUnitPrice(formattedUnitPrice);
        businessDataVO.setNewUsers(businessVO.getCountDayuser());
        return businessDataVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        return workspaceMapper.overviewSetmeals();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        return workspaceMapper.overviewDishes();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        return workspaceMapper.overviewOrders();
    }
}
